package gui;

import javax.swing.*;

import conference.Conference;
import conference.User;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Observable;

public class ChooseUserGUI extends Observable{
	private static final String TITLE = "Choose a User";
	private JPanel myPanel;
	private JComboBox myCBox;
	private Conference myConference;
	private User[] myUsers;
	private User myUser;
	
	public ChooseUserGUI(final Conference theConference) {
		myConference = theConference;
        myPanel = new JPanel(getBorderLayout());
        myUsers = myConference.getUsers();
		
        //Display the window.    
        setUpContents();
        myUser = myUsers[0];
        myPanel.setOpaque(true);
	}
	
	public JPanel getPanel() {
		return myPanel;
	}
	
	public User getUser() {
		return myUser;
	}
	
	private static BorderLayout getBorderLayout() {
		BorderLayout borderLayout = new BorderLayout();
    	borderLayout.setHgap(ChooseConferencePanel.BORDER_SPACE);
    	return borderLayout;
	}

	/**
	 * Creates and sets up components of Jpanel.
	 * @return
	 */
	private void setUpContents() {
        JLabel selectLabel = new JLabel("Please Select User: ");
 
        //Create the combo box
        ArrayList<String> myUserStrings = new ArrayList<String>(myUsers.length);
        int[] removeArray = new int[myUsers.length];
        for(int i = 0; i < myUsers.length; i++) {
        	if (!myUserStrings.contains(myUsers[i].getLastCommaFirst())) {
        		myUserStrings.add(myUsers[i].getLastCommaFirst());
        		removeArray[i] = 0;
        	}
        	else {
        		removeArray[i] = 1;
        	}
        }
        
        //match up the users to the user strings as indexes.
        User[] tempUserArray = new User[myUserStrings.size()];
        int index = 0;
        for (int i = 0; i < myUsers.length; i++) {
        	if (removeArray[i] == 0) {
        		tempUserArray[index] = myUsers[i];
        		System.out.println("Inside ChooseUGUI: User name is: " + tempUserArray[index].getLastCommaFirst() 
        				+ ", User String is: " + myUserStrings.get(index));
        		index++;
        	}
        }
        myUsers = tempUserArray;
        
        myUserStrings.trimToSize();
        
        myCBox = new JComboBox(myUserStrings.toArray(new String[myUserStrings.size()]));
        myCBox.setSelectedIndex(0);
        myCBox.addActionListener(
        	new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					myUser = myUsers[myCBox.getSelectedIndex()];
				}
        	}
        );
        System.out.println("Inside ChooseUGUI: Number of Strings: " + myUserStrings.size() + ", " + 
        		"Number of Users: " + myUsers.length);

        
        //Create Next Button
        JButton nextButton = new JButton("Next");
        nextButton.addActionListener(
        		new ActionListener(){
			public void actionPerformed(ActionEvent theEvent) {
				setChanged();
				notifyObservers(State.CHOOSE_USER);
				System.out.println("Selection is: " + myUser.getLastCommaFirst());
			}
        });
       
        //Lay out.
        myPanel.add(selectLabel, BorderLayout.WEST);
        myPanel.add(myCBox, BorderLayout.CENTER);
        JPanel eastPanel = new JPanel();
        eastPanel.add(nextButton);
        eastPanel.add(GuiController.getSwitchConferenceButton());
        myPanel.add(eastPanel, BorderLayout.EAST);
        myPanel.setBorder(BorderFactory.createEmptyBorder(ChooseConferencePanel.BORDER_SPACE ,ChooseConferencePanel.BORDER_SPACE, ChooseConferencePanel.BORDER_SPACE, ChooseConferencePanel.BORDER_SPACE));
	}
	
	public String getTitle() {
		return TITLE;
	}
}